package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-10-21
 * Time: 14:52
 */
@RequestMapping("/aaa")
@RestController
public class Hello {
    @RequestMapping("sayhi")
    public String sayHi(){
        return "hi, SpringBoot";
    }//

    @RequestMapping("/sayhello")
    public String sayHello(){
        return "hello, SpringBoot";
    }

    @RequestMapping("/saycat")
    public String saycat(){
        return "cat";
    }

    @RequestMapping(value = "/m1", method = RequestMethod.GET)//
    public String process(String name){
        return name;
    }

    @RequestMapping("/m2")
    public String process2(String name, Integer age){
        return  "名字 = " + name + "," + "年龄 = " + age;
    }

    @RequestMapping("/m3")
    public String process3(Person person){
        return  person.toString();
    }

    @RequestMapping("/m4")
    public String process4(){
        return "<h1>你好</h1>";
    }

    @RequestMapping("/m5")
    public String process5(@RequestParam(value = "name", required = false) String username){
        return "hahah";
    }

    @RequestMapping("/m6")
    public String process6(@RequestParam List<String> list){
        return  list.toString();
    }

    @RequestMapping("/m7")
    public String process7(@RequestBody Person person){//获取json数据
        return  person.toString();
    }

    @RequestMapping("/m8/{userId}")
    public Integer m8(@PathVariable Integer userId){//获取m8后面的url
        return userId;
    }

    @RequestMapping("/m9/{userId}/{name}")
    public String m9(@PathVariable Integer userId, @PathVariable("name") String username){//注意顺序要匹配
        return userId + username;
    }

    @RequestMapping("/m10")
    public String m10(@RequestPart MultipartFile file) throws IOException {
        file.transferTo(new File("C:/Users/33013/Desktop/番剧/" + file.getOriginalFilename()));//保存文件到C:/Users/33013/Desktop/番剧/
        return "success";//RequestPart是上传文件
    }

    @RequestMapping("/getcookie")
    public String m11(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            System.out.println(cookies[i]);
        }
        return "success";
    }

    @RequestMapping("/m12")
    public String m12(@CookieValue(required = false) String cookie){
        return cookie;//只能获取一个cookie
    }

    @RequestMapping("/m13")
    public String m13(@SessionAttribute(required = false) String session){
        return session;
    }
}
